#pragma once
#include "Machine.h"
#include "cpu/6502.h"
#include "apu/ay8910/ay8910.h"
#include "../Drawtile.h"

#define MirrorRAMAddr(aa)	(((aa % 32) * 32) + (aa / 32))
#define Decrypt(dd)			((dd & 0x9F) | ((dd & 0x20) << 1) | ((dd & 0x40) >> 1))

class CBurgerTimeMachine:public CMachine
{
public:
	C6502 *MainCPU;			//main cpu pointer
	C6502 *SoundCPU;			//sound cpu pointer
protected:
	u8 MainRAM[0x800];		//main cpu ram
	u8 SoundRAM[0x400];		//sound cpu ram
	u8 PaletteRAM[0x10];		//palette ram
	u8 VideoRAM[0x400];		//video ram
	u8 ColorRAM[0x400];		//color ram
	u8 MainROM[0x10000];		//main rom
	u8 SoundROM[0x10000];	//sound rom
	u8 NewROM[0x10000];		//ghetto decrypted rom
	u8 CHRROM[0x6000];		//tile rom
	u8 BGROM[0x1800];			//background rom
	u8 VROM[0x1000];			//extra background rom
	u8 VROM2[0x3000];			//extra extra background rom
	u8 VideoControl;			//video control register
	u8 SoundControl;			//sound control register
	u8 SoundIRQControl;		//irq control of sound cpu
	u8 Scroll;					//scroll register
	u8 VBlank;					//vblank status
	int Scanline;				//current scanline
	u32 Palette[0x40];		//converted palette
	u8 Screen[256 * (256+16)];	//screen drawn to
	u8 Temp[0x4000];
	u8 PROM[0x100];
	int HighScoresLoaded;
protected:
	void WritePalette(u32 addr,u8 data);
	void WriteSoundControl(u8 data);
	void CheckHighScores();
	void btimeDecrypt();
public:
	CBurgerTimeMachine();
	virtual ~CBurgerTimeMachine();
	virtual int Init();
	virtual void Kill();
	virtual void Reset();
	virtual void Blit();
	virtual void Line(u32 mainc,u32 soundc,int irq);
	virtual void Frame();
	virtual void SaveState(CState *s);
	virtual void LoadState(CState *s);
	virtual void SaveScores();
	virtual void LoadScores();
	virtual void RenderScreen(u8 *dest,int pitch);
	virtual void RenderBackground(u8 *dest,int pitch,int oor);	// render background layer
	void RenderSprites(u8 *dest,int pitch);							// render sprite layer
	void RenderChars(u8 *dest,int pitch,int mask=0,int oor=0);	// render character layer
	static u8 OpMainCPU(void *user,u32 addr);
	static u8 ReadMainCPU(void *user,u32 addr);
	static void WriteMainCPU(void *user,u32 addr,u8 data);
	static u8 ReadSoundCPU(void *user,u32 addr);
	static void WriteSoundCPU(void *user,u32 addr,u8 data);
	static void ProcessSound(void *user,void *data,int len);
	virtual int FPS(){return(57);}
};
